/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SaveTo.java
 *
 * Created on 17-feb-2011, 13:02:44
 */

package afterubuntu.view;

import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author Kermit
 */
public class SaveTo extends javax.swing.JDialog {

    private File file;

    /** Creates new form SaveTo */
    public SaveTo(AfterUbuntu parent, boolean modal) {
        super(parent, modal);
        initComponents();
        file = null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Save to...");

        jFileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        jFileChooser.setSelectedFile(new java.io.File("/home/adrian/afterUbuntu.sh"));
        jFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooserActionPerformed
        if(evt.getActionCommand().equalsIgnoreCase(javax.swing.JFileChooser.APPROVE_SELECTION)){
            if(jFileChooser.getSelectedFile().getAbsoluteFile().exists()){
                int result = JOptionPane.showConfirmDialog(this, "File already exists. Overwrite?", "Question", JOptionPane.YES_NO_CANCEL_OPTION);
                switch(result){
                    case JOptionPane.YES_OPTION:
                        file = jFileChooser.getSelectedFile().getAbsoluteFile();
                        this.dispose();
                        break;
                    case JOptionPane.NO_OPTION:
                        file = null;
                        this.dispose();
                        break;
                    case JOptionPane.CANCEL_OPTION:
                        this.dispose();
                        break;
                }
            }else{
                file = jFileChooser.getSelectedFile().getAbsoluteFile();
                this.dispose();
            }
        }if(evt.getActionCommand().equalsIgnoreCase(javax.swing.JFileChooser.CANCEL_SELECTION)){
            this.dispose();
        }

    }//GEN-LAST:event_jFileChooserActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jFileChooser;
    // End of variables declaration//GEN-END:variables

    File getFilePath() {
        return this.file;
    }


}
